<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="oneroom.user.addSeach.addressModel" %>
<jsp:useBean id="dao" scope="page" class="oneroom.user.zip.zipcode" />
<jsp:useBean id="adddao" 	scope="page" 	class="oneroom.user.addSeach.addressDao" />
<%
	List zipcode2List = dao.getZipcode2(); //주소 검색시 도를 보여준다.
	
	String pageName = "zip_list.jsp";
	String viewName = "zip_view.jsp";
	int     giSelectPage  	= Integer.parseInt(request.getParameter("txtSelectPage") == null ? "0" :request.getParameter("txtSelectPage"));			//현재 선택된 페이지 저장
	int     giSelectBlock 	= Integer.parseInt(request.getParameter("txtSelectBlock") == null ? "0" :request.getParameter("txtSelectBlock"));			//현재 선택된 블락페이지 저장
	//검색 부분
	String roomkind 	= request.getParameter("roomkind") 	== null ? "" : request.getParameter("roomkind");
	roomkind 			= roomkind.equals("no") ? "" : roomkind;
	String charter 		= request.getParameter("charter") 	== null ? "" : request.getParameter("charter");
	String zipcode2 	= request.getParameter("zipcode2") 	== null ? "" : request.getParameter("zipcode2");
	String zipcode3 	= request.getParameter("zipcode3") 	== null ? "" : request.getParameter("zipcode3");
	String zipcode4 	= request.getParameter("zipcode4") 	== null ? "" : request.getParameter("zipcode4");
	String deposit 		= request.getParameter("deposit") 	== null ? "0" : request.getParameter("deposit");
	String month 		= request.getParameter("month") 	== null ? "0" : request.getParameter("month");
	String sort 		= request.getParameter("sort") 		== null ? "date" : request.getParameter("sort");
	int     giCount       	= 0;			//리스트 for문 break문에 사용
	int     giRecordcnt   	= 0;			//리스트의 카운터에 사용
	int     giPageSize    	= 10;  			// 한페이지에 나타내고자 하는 List수
	int     giBlockSize   	= 10;   		// Block의 수
	int     giCurPage     	= 1;   			// 현재 Page
	int     giCurBlock    	= 0;   			// 현재 Block
	int     giStartPos    	= 0;   			// 시작 List 위치
	int     giTotPage     	= 0;   			// 총 Page수
	int     giTotBlock    	= 0;   			// 총 Block수
	//  넘겨온 값 받기(List페이지 관련)...
	if ( giSelectPage  >  0 ) { 			// 선택된 Page값이 있을경우....     
		giCurPage  = giSelectPage;   	// 현재Page = 선택된Page 
		giCurBlock = giSelectBlock;  	// 블락page = 선택된 블락 page
	}
	//쿼리를 돌려서 가지고 오는 값
	
	List roomList  = adddao.getAddress( roomkind, 
				 charter, 
				 zipcode2,
				 zipcode3,
				 zipcode4,
				 deposit, 
				 month,
				 sort,
				 giSelectPage, 
				 giPageSize);
	if (roomList.size() > 0 ) {
		//전체 게시물 수
		giRecordcnt = adddao.getCount(roomkind, 
									 charter, 
									 zipcode2,
									 zipcode3,
									 zipcode4,
									 deposit, 
									 month); 			// 총 list 수
	}
	if (giRecordcnt > 0) {  // 조건에 맞는 값이 있으면....
		giTotPage  = giRecordcnt / giPageSize;
		if (giRecordcnt % giPageSize != 0) {
			giTotPage = giTotPage + 1;			// 총 Page수를 구한다...
		}
		giTotBlock = giTotPage / giBlockSize;
		if (giTotPage % giBlockSize != 0) {
			giTotBlock = giTotBlock + 1;		// 총 Block수를 구한다...
		}
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/js/default.js"></script>
<script type="text/javascript" src="/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
	<%if(!zipcode2.equals("")){ //주소 검색을 하였을 경우 주소 선택한 항목에 보여주기 위한 작업 시 구주소가지고옴%>
	$.ajax({type : "post",
		url : "/room/zip/zipcode.jsp",
		dataType : "html",
		data : "zip_code2=<%=zipcode2%>&zip_code3=<%=zipcode3%>",
		success : function(msg){
			document.getElementById("zip3").innerHTML = msg;
		}
	});
	<%}%>
	<%if(!zipcode3.equals("")){ //주소 검색을 하였을 경우 주소 선택한 항목에 보여주기 위한 작업 읍면동을 가지고옴%>
	$.ajax({type : "post",
		url : "/room/zip/zipcode1.jsp",
		dataType : "html",
		data : "zip_code3=<%=zipcode3%>&zip_code4=<%=zipcode4%>",
		success : function(msg){
			document.getElementById("zip4").innerHTML = msg;
		}
	});
	<%}%>
	
	$('#zipcode2').change(function() {//AJAX로 주소 가지고옴 시 구를 가지고옴 
		$.ajax({type : "post",
			url : "/room/zip/zipcode.jsp",
			dataType : "html",
			data : "zip_code2="+document.getElementById("zipcode2").value,
			success : function(msg){
				document.getElementById("zip3").innerHTML = msg;
			}
		});
	});
	$('#seach').click(function() {
		document.getElementById("frm").submit();
	});
	$('#datesort').click(function() {
		document.getElementById("sort").value="date";
		document.getElementById("frm").submit();
	});
	$('#depositsort').click(function() {
		document.getElementById("sort").value="deposit";
		document.getElementById("frm").submit();
	});
	$('#monthsort').click(function() {
		document.getElementById("sort").value="month";
		document.getElementById("frm").submit();
	});
	
});

function zipCode3(){
	$('#zipcode3').change(function() {//AJAX로 주소 가지고옴 읍면동을 가지고옴
		$.ajax({type : "post",
			url : "/room/zip/zipcode1.jsp",
			dataType : "html",
			data : "zip_code3="+document.getElementById("zipcode3").value,
			success : function(msg){
				document.getElementById("zip4").innerHTML = msg;
			}
		});
	});
}

function goView(oneroomseq){
	document.frmView.oneroom_seq.value = oneroomseq;
	document.frmView.submit();
}
//-->
</script>
</head>
<body>
	<form action="zip_list.jsp" method="post" name="frm" id="frm">
	<input type="hidden" name="sort" value="<%=sort %>" id="sort" />
	<table style="width: 650px; border: 2px; border-color: #00000">
		<tr>
			<td>
			임대종류
			</td>
			<td>
			<select name="roomkind">
				<option value="no">선택</option>
				<option value="원룸" <%if(roomkind.equals("원룸")){out.print("selected='selected'");} %>>원룸</option>
				<option value="투/쓰리룸" <%if(roomkind.equals("투/쓰리룸")){out.print("selected='selected'");} %>>투/쓰리룸</option>
				<option value="오피스텔" <%if(roomkind.equals("오피스텔")){out.print("selected='selected'");} %>>오피스텔</option>
				<option value="단가임대" <%if(roomkind.equals("단가임대")){out.print("selected='selected'");} %>>단가임대</option>
				<option value="아파트" <%if(roomkind.equals("아파트")){out.print("selected='selected'");} %>>아파트</option>
				<option value="사무실" <%if(roomkind.equals("사무실")){out.print("selected='selected'");} %>>사무실</option>
			</select>
			</td>
			<td>
			임대형태
			</td>
			<td>
			<input type="radio" name="charter" value="J" <%if(charter.equals("") ||charter.equals("J")){out.print("checked='checked'");}  %>></input> 전세 
			<input type="radio" name="charter" value="W" <%if(charter.equals("W")){out.print("checked='checked'");}  %>></input> 월세 
			</td>
		</tr>
		<tr>
			<td>
			지역
			</td>
			<td colspan="3">
			<table style="width: 200px; border: 0px;">
				<tr>
					<td>
					<div id="zip2">
						<select name="zipcode2" id="zipcode2">
						<option value="선택">선택</option>
						<%
							for(int i = 0; i<zipcode2List.size(); i++){ 
								String zipcode2Value = (String)zipcode2List.get(i);
						%>
							<option value="<%=zipcode2Value %>" <%if(zipcode2.equals(zipcode2Value)){out.print("selected='selected'");} %>><%=zipcode2Value %></option>
						<%} %>
						</select>
					</div>
					</td>
					<td>
					<div id="zip3">	
						<select name="zipcode3" id="zipcode3">
							<option value="선택">선택</option>
						</select>
					</div>
					</td>
					<td>
					<div id="zip4">	
						<select name="zipcode4" id="zipcode4">
							<option value="선택">선택</option>
						</select>
					</div>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td>보증금
			</td>
			<td>
				<select name="deposit" id="deposit">
					<option value="100" <%if(deposit.equals("100")){out.print("selected='selected'");} %>>100~500</option>
					<option value="500" <%if(deposit.equals("500")){out.print("selected='selected'");} %>>500~1000</option>
					<option value="1000" <%if(deposit.equals("1000")){out.print("selected='selected'");} %>>1000~1500</option>
					<option value="1500" <%if(deposit.equals("1500")){out.print("selected='selected'");} %>>1500~2000</option>
					<option value="2000" <%if(deposit.equals("2000")){out.print("selected='selected'");} %>>2000~2500</option>
					<option value="2500" <%if(deposit.equals("2500")){out.print("selected='selected'");} %>>2500~3000</option>
					<option value="3000" <%if(deposit.equals("3000")){out.print("selected='selected'");} %>>3000~3500</option>
					<option value="3500" <%if(deposit.equals("3500")){out.print("selected='selected'");} %>>3500~4000</option>
					<option value="4000" <%if(deposit.equals("4000")){out.print("selected='selected'");} %>>4000~4500</option>
					<option value="4500" <%if(deposit.equals("4500")){out.print("selected='selected'");} %>>4500~5000</option>
					<option value="5000" <%if(deposit.equals("5000")){out.print("selected='selected'");} %>>5000~5500</option>
					<option value="5500" <%if(deposit.equals("5500")){out.print("selected='selected'");} %>>5500~6000</option>
					<option value="6000" <%if(deposit.equals("6000")){out.print("selected='selected'");} %>>6000~6500</option>
					<option value="6500" <%if(deposit.equals("6500")){out.print("selected='selected'");} %>>6500~7000</option>
					<option value="7000" <%if(deposit.equals("7000")){out.print("selected='selected'");} %>>7000~7500</option>
					<option value="7500" <%if(deposit.equals("7500")){out.print("selected='selected'");} %>>7500~8000</option>
					<option value="8000" <%if(deposit.equals("8000")){out.print("selected='selected'");} %>>8000~8500</option>
					<option value="8500" <%if(deposit.equals("8500")){out.print("selected='selected'");} %>>8500~9000</option>
					<option value="9000" <%if(deposit.equals("9000")){out.print("selected='selected'");} %>>9000~9500</option>
					<option value="9500" <%if(deposit.equals("9500")){out.print("selected='selected'");} %>>9500~10000</option>
					<option value="10000" <%if(deposit.equals("10000")){out.print("selected='selected'");} %>>10000~10500</option>
					<option value="10500" <%if(deposit.equals("10500")){out.print("selected='selected'");} %>>10500~1100</option>
				</select>만원
			</td>
			<td>월세
			</td>
			<td>
				<select name="month" id="month">
					<option value="1" <%if(month.equals("1")){out.print("selected='selected'");} %>>1~5</option>
					<option value="5" <%if(month.equals("5")){out.print("selected='selected'");} %>>5~10</option>
					<option value="10" <%if(month.equals("10")){out.print("selected='selected'");} %>>10~15</option>
					<option value="15" <%if(month.equals("15")){out.print("selected='selected'");} %>>15~20</option>
					<option value="20" <%if(month.equals("20")){out.print("selected='selected'");} %>>20~25</option>
					<option value="25" <%if(month.equals("25")){out.print("selected='selected'");} %>>25~30</option>
					<option value="30" <%if(month.equals("30")){out.print("selected='selected'");} %>>30~35</option>
					<option value="35" <%if(month.equals("35")){out.print("selected='selected'");} %>>35~40</option>
					<option value="40" <%if(month.equals("40")){out.print("selected='selected'");} %>>40~45</option>
					<option value="45" <%if(month.equals("45")){out.print("selected='selected'");} %>>45~50</option>
					<option value="50" <%if(month.equals("50")){out.print("selected='selected'");} %>>50~55</option>
					<option value="55" <%if(month.equals("55")){out.print("selected='selected'");} %>>55~60</option>
					<option value="60" <%if(month.equals("60")){out.print("selected='selected'");} %>>60~65</option>
					<option value="65" <%if(month.equals("65")){out.print("selected='selected'");} %>>65~70</option>
					<option value="70" <%if(month.equals("70")){out.print("selected='selected'");} %>>70~75</option>
					<option value="75" <%if(month.equals("75")){out.print("selected='selected'");} %>>75~80</option>
					<option value="80" <%if(month.equals("80")){out.print("selected='selected'");} %>>80~85</option>
					<option value="85" <%if(month.equals("85")){out.print("selected='selected'");} %>>85~90</option>
					<option value="90" <%if(month.equals("90")){out.print("selected='selected'");} %>>90~95</option>
					<option value="95" <%if(month.equals("95")){out.print("selected='selected'");} %>>95~100</option>
					<option value="100" <%if(month.equals("100")){out.print("selected='selected'");} %>>100~105</option>
					<option value="105" <%if(month.equals("105")){out.print("selected='selected'");} %>>10500~1100</option>
				</select>만원 
				<input type="button" id="seach" value="검색"/>
			</td>
		</tr>
	</table>
	<input type="button" id="datesort" value="최근등록순"/>
	<input type="button" id="depositsort" value="보증금순"/>
	<input type="button" id="monthsort" value="월세순"/>
	</form>
	<table style="width: 650px; border: 2px;">
	<%
		if(roomList != null && roomList.size() > 0){
			giCount  = 1;
			giRecordcnt = giRecordcnt - ( giCurPage -1 ) * giPageSize;	
			giStartPos = ((giCurPage - 1) * giPageSize) + 1;
	%>
	<%for(int i=0; i<roomList.size(); i++){ %>
	<% addressModel model = (addressModel)roomList.get(i);%>
	<Tr>
		<td>
			<%=giRecordcnt %>
		</td>
		<td>
		<%if(model.getPhoto1_main().equals("1")){ %>
			<img src="/download/<%=model.getPhoto1_small() %>?name=<%=model.getPhoto1_small() %>&kind=img" />
		<%}else if(model.getPhoto1_main().equals("2")){ %>
			<img src="/download/<%=model.getPhoto2_small() %>?name=<%=model.getPhoto2_small() %>&kind=img" />
		<%}else if(model.getPhoto1_main().equals("3")){ %>
			<img src="/download/<%=model.getPhoto3_small() %>?name=<%=model.getPhoto3_small() %>&kind=img" />
		<%}else if(model.getPhoto1_main().equals("4")){ %>
			<img src="/download/<%=model.getPhoto4_small() %>?name=<%=model.getPhoto4_small() %>&kind=img" />
		<%}else if(model.getPhoto1_main().equals("5")){ %>
			<img src="/download/<%=model.getPhoto5_small() %>?name=<%=model.getPhoto5_small() %>&kind=img" />
		<%}else if(model.getPhoto1_main().equals("6")){ %>
			<img src="/download/<%=model.getPhoto6_small() %>?name=<%=model.getPhoto6_small() %>&kind=img" />
		<%} %>
		</td>
		<td>
			<a href="javascript:goView('<%=model.getOneroom_seq() %>')"><%=model.getTitle() %></a>
		</td>
		<td>
			전세금<%=model.getRoommoney() %>
		</td>
		<td>
			월세<%=model.getRoommonth1() %>/<%=model.getRoommonth2() %>
		</td>
		<td>
			<%=model.getCreatedate() %>
		</td>
	</Tr>
	<%
		giCount = giCount + 1;
		giRecordcnt = giRecordcnt - 1;
	%>
	<%} %>
	<%}else{ %>
		<tr>
			<td>
				검색 된 내용이 없습니다.
			</td>
		</tr>
	<%} %>
	</table>
	<%@ include file="/room/common/zip_page.jsp" %>
	<form action="zip_view.jsp" method="post" name="frmView" id="framView">
		<input type="hidden" name="roomkind" value="<%=roomkind %>" />
		<input type="hidden" name="charter" value="<%=charter %>" />
		<input type="hidden" name="zipcode2" value="<%=zipcode2 %>" />
		<input type="hidden" name="zipcode3" value="<%=zipcode3 %>" />
		<input type="hidden" name="zipcode4" value="<%=zipcode4 %>" />
		<input type="hidden" name="deposit" value="<%=deposit %>" />
		<input type="hidden" name="month" value="<%=month %>" />
		<input type="hidden" name="sort" value="<%=sort %>" />
		<input type="hidden" name="oneroom_seq" value=""/>
	</form>
</body>
</html>